<?

/* USAGE
$babel = new Babel('ita', true);
echo __('ciao %s %s', 'alb', 'ben');

// ...

// at the end of your page
$babel->save();

*/


@include_once("../_inc/configure.php"); 
@include_once("_inc/configure.php"); 

class Babel
{

	var $apprendi;
	var $lingua;
	var $chiavi;
	var $dizionario;
	
	function Babel($lingua='ita', $apprendi=false)
	{
		global $BABEL_DIR;
		$this->apprendi	=	$apprendi;
		$this->lingua	=	$lingua;
		$this->chiavi	=	unserialize(file_get_contents($BABEL_DIR."/chiavi.dat"));
		$this->dizionario=	unserialize(file_get_contents($BABEL_DIR."/".$this->lingua.".dat"));
	}
	
	
	function translate($args, $safe=false)
	{
		$ret = $testo = array_shift($args);
		$chiave = $this->chiavi[$testo];
		if($chiave>0) 
		{
			if($_REQUEST[babel])
			{
				if($safe) 	$ret	=	$this->dizionario[$chiave];
				else 		$ret 	= 	$this->get_translation_link($chiave,"[T]").$this->dizionario[$chiave];
			}
			else $ret = $this->dizionario[$chiave];
			if(!(strlen($ret)>0))
			{
				if($safe) 	$ret	=	$chiave;
				else 		$ret 	= 	$this->get_translation_link($chiave,"[#]").$chiave;
			}
		}
		else 
		{
			// APPRENDI NUOVA CHIAVE SE IN ITA
			if($this->apprendi && $this->lingua=="ita")
			{
				$prossima_chiave = count($this->chiavi);
				$this->chiavi[$testo] = $prossima_chiave;
				$this->dizionario[$prossima_chiave] = $testo; 	
				if($safe) 	$ret	=	$testo;
				else 		$ret 	= 	$this->get_translation_link($prossima_chiave, "[!]").$testo	;
			}
			else $ret = "[+]".$testo;
		}
		array_unshift($args, $ret);
		// sostituisco parametri
		$ret = call_user_func_array('sprintf',$args);
		
		return $ret;
	}
	

	
	function save()
	{
		global $BABEL_DIR;
		if($this->apprendi && $this->lingua=="ita")
		{
			if(file_put_contents($BABEL_DIR."/".$this->lingua.".dat",serialize($this->dizionario)) && file_put_contents($BABEL_DIR."/chiavi.dat",serialize($this->chiavi)))
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
	}
	function get_translation_link($chiave, $simbolo)
	{
		return "<a href=\"javascript: load = window.open('/_class/Babel.class.php?mod=mod&chiave=$chiave','','scrollbars=no,menubar=no,height=400,width=800,resizable=yes,toolbar=no,location=no,status=no');load.focus();\">$simbolo</a>";
	}
	
}





class BabelAdmin
{
	var $chiavi;
	var $ita;
	var $eng;
	
	function BabelAdmin()
	{
		global $BABEL_DIR;
		$this->chiavi	=	unserialize(file_get_contents($BABEL_DIR."/chiavi.dat"));
		$this->ita	=	unserialize(file_get_contents($BABEL_DIR."/ita.dat"));
		$this->eng	=	unserialize(file_get_contents($BABEL_DIR."/eng.dat"));
	}
	
	function get($chiave, $lingua)
	{
		return $this->{$lingua}[$chiave];
	}
	
	function set($chiave, $lingua, $valore)
	{
		global $BABEL_DIR;
		if($lingua!="ita") 
		{
			$this->{$lingua}[$chiave] = $valore;
			file_put_contents($BABEL_DIR."/".$lingua.".dat",serialize($this->{$lingua})) ; 
		}
	}
	
	
	
}

function __($testo)
{
	global $babel;
	return $babel->translate(func_get_args());
}

function _s($testo)
{
	global $babel;
	return $babel->translate(func_get_args(),true);
}




if($_GET[mod])
{
	$babel = new BabelAdmin();
	if($_REQUEST[mod]=="salva")
	{
		$babel->set($_REQUEST[chiave],$_REQUEST[lingua], $_REQUEST[valore]);
	}
	$chiave = $_REQUEST[chiave];
	if($chiave>0)
	{

?>	
 <table width="90%" border="0">
    <tr>
      <td valign="middle">ITA</td>
      <td valign="middle">
      <form id="form1" name="form1" method="post" action="">
        <textarea name="valore" cols="44" rows="3" id="textfield"><?php echo $babel->get($chiave, 'ita')?></textarea>
        <input type="hidden" name="chiave" value="<? echo $chiave ?>" />
        <input type="hidden" name="mod" value="salva" />
        <input type="hidden" name="lingua" value="ita" />
      <input type="submit" name="button" id="button" value="Salva" />
      </form></td>
    </tr>
    <tr>
      <td valign="middle">ENG</td>
      <td valign="middle"> <form id="form1" name="form1" method="post" action="">
        <textarea name="valore" cols="44" rows="3" id="eng"><?php echo $babel->get($chiave, 'eng')?></textarea>
        <input type="hidden" name="chiave" value="<? echo $chiave ?>" />
        <input type="hidden" name="mod" value="salva" />
        <input type="hidden" name="lingua" value="eng" />
      <input type="submit" name="button2" id="button2" value="Salva" />
      </form></td>
    </tr>
    <tr>
      <td valign="middle">&nbsp;</td>
      <td valign="middle"><input type="button" onclick="javascript: opener.location = opener.location; self.close();" name="button2" id="button2" value="Chiudi" /></td>
    </tr>
  </table>
<? 	
	}
}

?>